home *** CD-ROM | disk | FTP | other *** search
- // layered2.cpp : アプリケーション用のエントリ ポイントの定義
- //
-
- #include "stdafx.h"
- #include "layeredwnd.h"
-
- #define APPNAME "LayeredWindow2"
- static char szAppName[] = APPNAME;
- static char szTitle[] = APPNAME;
-
- #define MM_QUIT WM_USER+1
-
- LayeredWnd *playeredWnd;
-
- HINSTANCE hInst; // USER32.DLL のインスタンスハンドル
- // SetLayeredWindowAttributes のアドレス
- BOOL (WINAPI *_SetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
-
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow )
- {
- // TODO: この位置にコードを記述してください。
- OSVERSIONINFO versioninfo;
- versioninfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- GetVersionEx( &versioninfo );
- if( versioninfo.dwMajorVersion<5 ){
- MessageBox( NULL, "このバージョンのWindowsでは動作しません。", "LayeredWindow",MB_OK|MB_ICONHAND );
- return FALSE;
- }
- if( !InitInstance( hInstance ) ){
- MessageBox( NULL, "アプリケーションの初期化に失敗しました。", "LayeredWindow", MB_OK|MB_ICONHAND );
- return FALSE;
- }
-
- MSG msg;
- while( GetMessage( &msg, NULL, 0, 0 ) ){
- TranslateMessage( &msg ); // Translates virtual key codes.
- DispatchMessage( &msg ); // Dispatches message to window.
- }
- ExitInstance();
- return msg.wParam; // Returns the value from PostQuitMessage.
- }
-
- BOOL InitInstance( HINSTANCE hInstance )
- {
- // SetLayeredWindowAttributes のエクスポートアドレスの取得
- hInst = LoadLibrary( "USER32.DLL" );
- if( hInst ){
- _SetLayeredWindowAttributes = (BOOL(WINAPI*)(HWND,COLORREF,BYTE,DWORD))
- GetProcAddress( hInst, "SetLayeredWindowAttributes" );
- }
- if( _SetLayeredWindowAttributes==NULL ) return FALSE;
-
- playeredWnd = new LayeredWnd( hInstance );
- HWND hWnd = playeredWnd->Create();
- return hWnd?TRUE:FALSE;
- }
-
- void ExitInstance()
- {
- if( playeredWnd ) delete playeredWnd;
- if( hInst ) FreeLibrary( hInst );
- }
-
- LONG APIENTRY MainWndProc( HWND hWnd, UINT message, UINT wParam, LONG lParam )
- {
- if( message==WM_NCCREATE ){
- CREATESTRUCT *pcs = (CREATESTRUCT*)lParam;
- SetWindowLong( hWnd, GWL_USERDATA, (LONG)pcs->lpCreateParams );
- }
- LayeredWnd *pWnd = (LayeredWnd*)GetWindowLong( hWnd, GWL_USERDATA );
- if( pWnd==NULL ){
- return DefWindowProc( hWnd, message, wParam, lParam );
- } else {
- return pWnd->WndProc( hWnd, message, wParam, lParam );
- }
- return 0;
- }
-
- //////////////////////////////////////////////////////////////////////
- // LayeredWnd クラス
- //////////////////////////////////////////////////////////////////////
-
- //////////////////////////////////////////////////////////////////////
- // 構築/消滅
- //////////////////////////////////////////////////////////////////////
-
- LayeredWnd::LayeredWnd( HINSTANCE hInstance )
- {
- WNDCLASSEX wc;
- wc.cbSize = sizeof( WNDCLASSEX );
- wc.style = 0;
- wc.lpfnWndProc = (WNDPROC)MainWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
- wc.hCursor = LoadCursor( NULL, IDC_ARROW );
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszMenuName = NULL;
- wc.lpszClassName = szAppName;
- wc.hIconSm = NULL;
- RegisterClassEx( &wc );
- m_hInstance = hInstance;
-
- m_hMenu = CreatePopupMenu();
- MENUITEMINFO mi;
- mi.cbSize = sizeof(MENUITEMINFO);
- mi.fMask = MIIM_ID|MIIM_TYPE;
- mi.fType = MFT_STRING;
- mi.wID = MM_QUIT;
- mi.dwTypeData = "終了";
- mi.cch = strlen( mi.dwTypeData );
- InsertMenuItem( m_hMenu, 0, TRUE, &mi );
-
- m_hBitmap = LoadBitmap( m_hInstance, MAKEINTRESOURCE(IDB_OHX) );
- BITMAPINFOHEADER *pbmih = (BITMAPINFOHEADER*)GlobalAlloc( GPTR, sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256 );
- memset( pbmih, 0, sizeof(BITMAPINFOHEADER) );
- pbmih->biSize = sizeof(BITMAPINFOHEADER);
- HDC hDC = GetDC( NULL );
- GetDIBits( hDC, m_hBitmap, 0, 0, NULL, (BITMAPINFO*)pbmih, DIB_RGB_COLORS );
- ReleaseDC( NULL, hDC );
- m_nWidth = pbmih->biWidth;
- m_nHeight = pbmih->biHeight;
- GlobalFree( pbmih );
- }
-
- LayeredWnd::~LayeredWnd()
- {
- DestroyMenu( m_hMenu );
- }
-
- HWND LayeredWnd::Create()
- {
- CREATESTRUCT cs;
- cs.lpCreateParams = this;
- cs.hInstance = m_hInstance;
- cs.hMenu = NULL;
- cs.hwndParent = NULL;
- cs.cy = m_nHeight;
- cs.cx = m_nWidth;
- cs.y = CW_USEDEFAULT;
- cs.x = CW_USEDEFAULT;
- cs.style = WS_POPUP|WS_VISIBLE;
- cs.lpszName = szTitle;
- cs.lpszClass = szAppName;
- cs.dwExStyle = WS_EX_TOOLWINDOW|WS_EX_LAYERED; // レイヤードウィンドウ
- m_hWnd = CreateWindowEx( cs.dwExStyle, cs.lpszClass, cs.lpszName, cs.style,
- cs.x, cs.y, cs.cx, cs.cy, cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams );
- return m_hWnd;
- }
-
- LRESULT LayeredWnd::WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
- {
- switch( message ){
- case WM_CREATE:
- OnCreate( hWnd );
- return 0;
- case WM_DESTROY:
- OnDestroy();
- PostQuitMessage( 0 );
- return 0;
- case WM_PAINT:
- OnPaint( (HDC)wParam );
- return 0;
- case WM_NCHITTEST:
- return OnNcHitTest( LOWORD(lParam), HIWORD(lParam) );
- case WM_NCRBUTTONDOWN:
- OnNcRButtonDown( wParam, MAKEPOINTS(lParam) );
- return 0;
- case WM_COMMAND:
- switch( LOWORD(wParam) ){
- case MM_QUIT:
- DestroyWindow( m_hWnd );
- return 0;
- }
- break;
- }
- return DefWindowProc( hWnd, message, wParam, lParam );
- }
-
- void LayeredWnd::OnCreate( HWND hWnd )
- {
- _SetLayeredWindowAttributes( hWnd, RGB(0,0,0), 128, LWA_COLORKEY|LWA_ALPHA );
- }
-
- void LayeredWnd::OnDestroy()
- {
- }
-
- void LayeredWnd::OnPaint( HDC hDC )
- {
- PAINTSTRUCT ps;
- hDC = BeginPaint( m_hWnd, &ps );
- HDC hMemDC = CreateCompatibleDC( NULL );
- SelectObject( hMemDC, m_hBitmap );
- BitBlt( hDC, 0, 0, m_nWidth, m_nHeight, hMemDC, 0, 0, SRCCOPY );
- DeleteDC( hMemDC );
- EndPaint( m_hWnd, &ps );
- }
-
- int LayeredWnd::OnNcHitTest( int xPos, int yPos )
- {
- return HTCAPTION;
- }
-
- void LayeredWnd::OnNcRButtonDown( int nHittest, POINTS pts )
- {
- TrackPopupMenu( m_hMenu, TPM_LEFTALIGN|TPM_TOPALIGN, pts.x, pts.y, 0, m_hWnd, NULL );
- }
-